﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BlueCMS.Core.Data;
using BlueCMS.Core.Data.Query;
using BlueCMS.Core.Entities;

namespace BlueCMS.Core.Services
{
    class WebpageService
    {
        public static Webpage GetWebpageForNode(Node node)
        {
            Webpage webpage;

            SelectQuery query = new SelectQuery();
            query.Select.Column("w.*");
            query.From.Table("cms_webpage_tbl").As("w")
                .Join("cms_webpage_node_lnk").As("l").On(c => c.Column("l.webpage_id").Equals().Column("w.webpage_id"));
            query.Where
                .Column("l.node_id").Equals().Parameter(node.node_id)
                .And()
                .Column("w.isactive").Equals().Constant(true)
                .And()
                .Column("w.isdeleted").Equals().Constant(false);

            webpage = query.ExecuteObject<Webpage>();

            if (webpage != null)
            {
                webpage.Template = TemplateService.GetTemplateForId(webpage.template_id);
            }

            return webpage;
        }
    }
}
